<?php
class Sms_mdl extends CI_Model{
  //操作的表.
  const TAB_NAME='clickrcms_sms';

  function __construct(){
    parent::__construct();
    log_message('debug','sms_mdl Model class Initialized');
  }

  /****
   *查詢所sms,加分页
   *$data :查询参数.
   */
  public function get_sms($data=array()){
    extract($data);
    isset($field) && isset($sort)?$this->db->order_by($field.' '.$sort):$this->db->order_by('id DESC');
    isset($student_id) && !empty($student_id)?$this->db->where('student_id',$student_id):'';
    isset($phone_number) && !empty($phone_number)?$this->db->like("phone_number",$phone_number):'';
    isset($limits) && isset($offset)?$this->db->limit($limits,$offset):'';
    isset($send_date) && !empty($send_date)?$this->db->where('send_date >=',strtotime($send_date.' 00:00:00')):'';
    isset($send_date) && !empty($send_date)?$this->db->where('send_date <=',strtotime($send_date.' 23:59:59')):'';
    isset($admin_name) && !empty($admin_name)?$this->db->like('admin_name',$admin_name):'';
    isset($send_result) && !empty($send_result)?$this->db->like('send_result',$send_result):'';
    isset($phone_area) && !empty($phone_area)?$this->db->where('phone_area',$phone_area):'';
    isset($begin_month) && !empty($begin_month) && isset($end_month) && !empty($end_month)?$this->db->where('send_date BETWEEN "'.$begin_month.'" AND "'.$end_month.'"',NULL,FALSE):'';//allocation,index
    return $this->db->get(self::TAB_NAME);
  }
  /****
   * 統計所有的sms
   */
  public function totle_sms($data=array()){
    if(!empty($data)){
      extract($data);
      isset($student_id) && !empty($student_id)?$this->db->where('student_id',$student_id):'';
      isset($phone_number) && !empty($phone_number)?$this->db->like("phone_number",$phone_number):'';
      isset($send_date) && !empty($send_date)?$this->db->where('send_date >=',strtotime($send_date.' 00:00:00')):'';
      isset($send_date) && !empty($send_date)?$this->db->where('send_date <=',strtotime($send_date.' 23:59:59')):'';
      isset($admin_name) && !empty($admin_name)?$this->db->like('admin_name',$admin_name):'';
      isset($send_result) && !empty($send_result)?$this->db->like('send_result',$send_result):'';
      isset($phone_area) && !empty($phone_area)?$this->db->where('phone_area',$phone_area):'';
      isset($begin_month) && !empty($begin_month) && isset($end_month) && !empty($end_month)?$this->db->where('send_date BETWEEN "'.$begin_month.'" AND "'.$end_month.'"',NULL,FALSE):'';//allocation,index
      return $this->db->count_all_results(self::TAB_NAME);
    }else{
      return $this->db->count_all(self::TAB_NAME);
    }
  }
  
  /*****
   * 根據ID來刪除
   */
  public function delete_sms($delete_id=''){
    if(!empty($delete_id)){
      $this->db->where("id",$delete_id);
      $this->db->delete(self::TAB_NAME);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }

  /***
   * 修改一個sms
   * @param array $data
   * @param int  $edit_id
   * return :受影响的行数
   */
  public function edit_sms($data=array(),$edit_id=0){
    if(!empty($data) && !empty($edit_id)){
      $this->db->where('id',$edit_id);
      $this->db->update(self::TAB_NAME,$data);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }

  /***
   * 新增一個 sms
   */
  public function add_sms($data=array()){
    if(!empty($data)){
      if(isset($data['phone_area']) && $data['phone_area']=='店主'){
        $data['message_txt']=str_replace('%%%00','',$data['message_txt']);
        $data['message_txt']=str_replace('%%%10','',$data['message_txt']);
        $data['message_txt']=str_replace('%%','',$data['message_txt']);
        $data['message_txt']=str_replace('-','',$data['message_txt']);
        $data['message_txt']=str_replace(' ','',$data['message_txt']);
      }
      $this->db->insert(self::TAB_NAME,$data);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }

  /****
   *根据ID来查询得到一个 sms.
   *$id :要查詢的ID
   */
  public function get_sms_by_id($id){
    $data=array();
    $this->db->where('id',$id);
    $get_result=$this->db->get(self::TAB_NAME);
    if($get_result->num_rows()>0)
    {
      $data=$get_result->row_array();
    }
    $get_result->free_result();
    return $data;
  }

  /***
   * 清空 operator
   */
  public function empty_table(){
    $this->db->empty_table(self::TAB_NAME);
    return ($this->db->affected_rows()>0)?true:false;
  }
}